#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>




#define FTOK_PATH "/dev/zero"
#define FTOK_PROJID 0x11

typedef struct shared_memory
{
	int		age;
	char	name[64];
} s_m;

int main(int argc, char **argv)
{
	int					rv = -1;
	key_t				ftok_key = -1;
	int					shmid = -1;
	s_m					*ps_m = NULL;
	int					i =  0;
	
	ftok_key = ftok(FTOK_PATH, FTOK_PROJID);
	if(ftok_key < 0)
	{
		printf("ftok error : %s \n",strerror(errno));
		return -1;
		
	}

	shmid = shmget(ftok_key, sizeof(s_m), IPC_CREAT | 0666);
	if( shmid  < 0)
	{
		printf("shmget error : %s \n",strerror(errno));
		return -2;
	}		

	ps_m = shmat(shmid, NULL, 0);
	if( (void *)-1 == ps_m)
	{
		printf("shmat error : %s \n",strerror(errno));
		return -3;
	}

	strncpy(ps_m->name, "guanfuxin", sizeof(ps_m->name));
	ps_m->age = 18;

	for(i = 0; i < 4; i++)
	{
		ps_m->age ++;
		printf(" %s : age : %d \n", ps_m->name, ps_m->age);
		sleep(1);
	}

	rv = shmdt(ps_m);


	return 0;

	
}





